import sys, string
from arkaya import Rom
from xml.sax import handler, make_parser
		
class RomSaxDocumentHandler(handler.ContentHandler):
	def __init__(self, roms):
		self.isRomElement = 0
		self.isTitleElement = 0
		self.isDescriptionElement = 0
		self.isRomNameElement = 0
		self.isPathElement = 0
		self.isYearElement = 0
		self.isAuthorElement = 0
		self.roms = roms
		self.rom = None
				
	def startElement(self, name, attrs):
		if name == 'rom':
			self.rom = Rom()
		elif name == 'title':
			self.isTitleElement = 1
		elif name == 'description':
			self.isDescriptionElement = 1
		elif name == 'rom_name':
			self.isRomNameElement = 1
		elif name == 'path':
			self.isPathElement = 1
		elif name == 'year':
			self.isYearElement = 1
		elif name == 'author':
			self.isAuthorElement = 1
						
	def endElement(self, name):
		if name == 'rom':
			self.roms.append(self.rom)
			self.rom = None
			self.isRomElement = 0
		elif name == 'title':
			self.isTitleElement = 0
		elif name == 'description':
			self.isDescriptionElement = 0
		elif name == 'rom_name':
			self.isRomNameElement = 0
		elif name == 'path': 
			self.isPathElement = 0
		elif name == 'year':
			self.isYearElement = 0
		elif name == 'author':
			self.isAuthorElement = 0
				
	def characters(self, chrs):
		if self.isTitleElement:
			self.rom.title = chrs 
		elif self.isDescriptionElement:
			self.rom.description = chrs 
		elif self.isRomNameElement:
			self.rom.rom_name = chrs
		elif self.isPathElement:
			self.rom.path = chrs
		elif self.isYearElement:
			self.rom.year = chrs
		elif self.isAuthorElement: 
			self.rom.author = chrs
